class Register {
    constructor() {
        this.btn = document.querySelector('.btn');
        this.username = document.querySelector('.username');
        this.phone = document.querySelector('.phone');
        this.password = document.querySelector('.password');
        this.checkpassword = document.querySelector('.checkpassword');
        this.usernameJudge = false; // 判断是否有相同的用户名，有为true
        this.phoneJudge = false;
        this.clickRegister();
        this.judgeUsername();
        this.judgePhone();

    }
    clickRegister() {
        this.btn.onclick = () => {
            // 判断所有的表单是否填写完整

            if (!this.username.value || !this.phone.value || !this.password.value || !this.checkpassword.value) {
                alert('请把信息填写完整');
                return
            }
            // 判断两次密码是否一致
            if (this.password.value != this.checkpassword.value) {
                alert('两个密码不一致')
                return
            }

            if (!this.phoneJudge && !this.usernameJudge) {
                var obj = {
                    username: this.username.value,
                    phone: this.phone.value,
                    password: this.password.value,
                    checkpassword: this.checkpassword.value
                }
                // 存数据
                var arr = JSON.parse(localStorage.getItem('info')) || []
                // JSON.stringify(localStorage.setItem('info'))
                arr.push(obj);
                localStorage.setItem('info', JSON.stringify(arr));
                this.username.value = '';
                this.phone.value = '';
                this.password.value = '';
                this.checkpassword.value = '';
                alert('注册成功！两秒后将跳转至登录');
                setTimeout(() => {
                    location.href = 'http://localhost:2206/login.html';
                })
            } else {
                alert('密码不一致');
                return
            }
        }
    }
    // 判断用户名是否重复
    judgeUsername() {
        this.username.onblur = () => {
            let arr = JSON.parse(localStorage.getItem('info')) || [];
            arr.forEach(item => {
                if (this.username.value == item.username) {
                    alert('该用户名已经被注册！换一个名字吧！');
                    return
                }
            });
            this.usernameJudge = arr.some((item) => {
                return this.username.value == item.username
            })
        }
    }
    // 判断电话号码是否重复
    judgePhone() {
        let arr = JSON.parse(localStorage.getItem('info')) || [];
        this.phoneJudge = arr.some((item) => {
            return this.phone.value == item.phone.value
        })
        if (this.phoneJudge) {
            alert('该手机已经被注册');
        }
    }
}
new Register();